package com.lifeng.controller;

import com.lifeng.dto.CarDto;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/car")
public class CarController {
    private Map<String,CarDto> data = new HashMap<>();
    @RequestMapping("/add")
    public Object addCar(CarDto carDto){
        if(data.get(carDto.getId()) == null){
            data.put(carDto.getId(),carDto);
        }else{
            data.get(carDto.getId()).setCount(data.get(carDto.getId()).getCount() + carDto.getCount());
        }
        return true;
    }

    @RequestMapping("/list")
    public Object list(){
        return data.values();
    }

    @RequestMapping("/editSelected")
    public Object editSelected(String id){
        if(data.get(id) != null){
            data.get(id).setSelected(!data.get(id).getSelected());
        }
        return true;
    }

    @RequestMapping("/editCount")
    public Object editCount(String id,Integer count){
        if(data.get(id) != null){
            data.get(id).setCount(count);
        }
        return true;
    }

    @RequestMapping("/delete")
    public Object delete(String id){
        data.remove(id);
        return true;
    }
}
